package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.dao.core.UserAdaPayMoneyDao;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.newEntity.UserAdaPayMoney;
import com.tbit.uqbike.client.service.UserAdaPayMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: chen
 * @Description: 用户金额分布
 * @Date: 创建于 10:49 2022/5/16
 */
@Service
public class UserAdaPayMoneyServiceImpl implements UserAdaPayMoneyService {

    @Autowired
    private UserAdaPayMoneyDao userAdaPayMoneyDao;

    @Override
    public UserAdaPayMoney getByUserId(Integer userId) {
        return userAdaPayMoneyDao.getByUserId(userId);
    }

    @Override
    public void add(User user, Integer amount, Integer adaPayMoney) {
        UserAdaPayMoney userAdaPayMoney = getByUserId(user.getUserId());
        if (userAdaPayMoney == null) {
            userAdaPayMoney = new UserAdaPayMoney();
            userAdaPayMoney.setPhone(user.getPhone());
            userAdaPayMoney.setUserId(user.getUserId());
            userAdaPayMoney.setAccountId(user.getAccountId());
            userAdaPayMoney.setAddTime(new Date());
            if (adaPayMoney < 0) {
                adaPayMoney = 0;
            }
            userAdaPayMoney.setAdaPayMoney(adaPayMoney);
            if (amount < 0) {
                amount = 0;
            }
            userAdaPayMoney.setAmount(amount);
            userAdaPayMoneyDao.insert(userAdaPayMoney);
        } else {
            if (userAdaPayMoney.getAmount() + amount < 0) {
                userAdaPayMoney.setAmount(-userAdaPayMoney.getAmount());
            } else {
                userAdaPayMoney.setAmount(amount);
            }
            if (userAdaPayMoney.getAdaPayMoney() + adaPayMoney < 0) {
                userAdaPayMoney.setAdaPayMoney(-userAdaPayMoney.getAdaPayMoney());
            } else {
                userAdaPayMoney.setAdaPayMoney(adaPayMoney);
            }
            userAdaPayMoney.setPhone(user.getPhone());
            userAdaPayMoneyDao.edit(userAdaPayMoney);
        }

    }

    public Integer moneyCheck(Integer userId, Integer money) {


        return null;
    }

}
